
package com.crv.ci.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.crv.common.domain.view.BizData4Page;
import com.crv.ci.model.PublishTodo;
import com.crv.ci.service.IPublishTodoService;
import com.crv.common.utils.IdGenerator;
import com.crv.common.utils.JsonResult;
import com.crv.common.utils.PageParam;
import com.crv.common.utils.Constants;

@Controller
public class PublishTodoAction extends AbstractAdminController<IPublishTodoService>{

    @Autowired
    private IPublishTodoService publishTodoService;
    
    @RequestMapping(value="/publishtodo/list")
    public String list(PublishTodo publishTodo,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(publishTodo); 	
    	BizData4Page<PublishTodo> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("publishTodo", publishTodo);
    	return "/module/publishtodo/list";
    }    
    
    @RequestMapping(value="/publishtodo/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("publishTodo", new PublishTodo());
        return "module/publishtodo/edit";
    }    
    
    @RequestMapping(value="/publishtodo/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	PublishTodo publishTodo = publishTodoService.fetch(id);
    	model.addAttribute("publishTodo", publishTodo);
        return "module/publishtodo/edit";
    }    
    
    @RequestMapping(value="/publishtodo/save")
    @ResponseBody
    public String save(HttpServletRequest request,PublishTodo publishTodo){
		boolean isAddNew = StringUtils.isBlank(publishTodo.getId())?true:false;
		if(isAddNew)
		{
			publishTodo.setId(IdGenerator.createNewId());
			publishTodoService.add(publishTodo);
		}
		else
		{
			publishTodoService.update(publishTodo);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/publishtodo/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	publishTodoService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(PublishTodo publishTodo)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("publishName", publishTodo.getPublishName());		
		conditions.put("agentId", publishTodo.getAgentId());		
		conditions.put("groupId", publishTodo.getGroupId());		
		conditions.put("status", publishTodo.getStatus());		
		conditions.put("publishType", publishTodo.getPublishType());		
		conditions.put("templateId", publishTodo.getTemplateId());		
		conditions.put("publishFile", publishTodo.getPublishFile());		
		conditions.put("isCron", publishTodo.getIsCron());		
		conditions.put("cronExpr", publishTodo.getCronExpr());		
		conditions.put("cronDesc", publishTodo.getCronDesc());		
		conditions.put("note", publishTodo.getNote());		
		conditions.put("createrId", publishTodo.getCreaterId());		
		conditions.put("createrName", publishTodo.getCreaterName());		
		conditions.put("publishDate", publishTodo.getPublishDate());		
		conditions.put("createDate", publishTodo.getCreateDate());		
    	return conditions;
    }

    @Override
    protected IPublishTodoService getMainService() {
        return publishTodoService;
    }

    @Override
    protected String getMainObjName() {
        return "publishtodo";
    }

    @Override
    protected String getViewTitle() {
        return "publishtodo";
    }
}
